"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 目录不存在	双机场景：主机、备机创建表空间时所需目录不存在
Create At   : 2020/07/02
@zou_jialiang0505328114
Description :
    1、启动数据库
    2、验证不存在目录
    3、主机使用不存在目录创建表空间
    4、创建用户无中间级权限目录
    5、主机无中间级权限目录创建表空间
    6、验证不存在目录
    7、主机使用不存在目录创建表空间
    8、备机重建
Expect      :
    1、启动成功，数据库状态正常
    2、执行成功
    3、执行成功
    4、执行失败
    5、执行失败
    6、执行成功
    7、执行成功
    8、执行成功
History     :
    2020/12/7 n@ningyali 1、优化脚本；2、teardown中增加主备机对表、表空间及表空间路径的删除
"""

import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_standby = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case026.py start------------')
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.primary_root = Node(node='PrimaryRoot')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.standby2_root = Node(node='Standby2Root')
        self.com = Common()
        self.Constant = Constant()
        self.path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH), 'dbsys026_tablespace')
        self.path1 = os.path.join(self.path, 'dbsys026_tablespace')

    def test_startdb(self):
        self.log.info('------------------------step2:确保目录不存在------------------------')
        self.log.info('rm -rf ' + self.path)
        self.primary_root.sh('rm -rf ' + self.path)

        self.log.info('----------------step3:主机使用不存在目录创建表空间 success------------------------------')
        createdb = f'''drop tablespace if exists dbsys026_tbl; create tablespace dbsys026_tbl location '{self.path}';'''
        createdbresult = self.sh_primy.executDbSql(createdb)
        self.log.info(createdbresult)
        self.assertIn('CREATE TABLESPACE', createdbresult)

        self.log.info('-------------------drop tblspace------------------------')
        cmd = f'''drop tablespace dbsys026_tbl;'''
        msg = self.sh_primy.executDbSql(cmd)
        self.log.info(msg)
        self.assertIn('DROP TABLESPACE', createdbresult)

        time.sleep(5)

        self.log.info('----------------------step4:创建用户无中间级权限目录---------------------------')
        self.log.info('mkdir -p ' + self.path1)
        msg = self.primary_dbuser.sh('mkdir -p ' + self.path1).result()
        self.log.info(msg)
        self.log.info('chmod 000 ' + self.path)
        msg = self.primary_dbuser.sh('chmod 000 ' + self.path).result()
        self.log.info(msg)

        self.log.info('------------------------step5:主机无中间级权限目录创建表空间 fail------------------------')
        createdb = f'''drop tablespace if exists dbsys026_tb2; create tablespace dbsys026_tb2 location '{self.path1}';'''
        createdbresult = self.sh_primy.executDbSql(createdb)
        self.log.info(createdb)
        self.log.info('表空间创建失败，给出明确提示；')
        self.log.info(createdbresult)
        self.assertIn('Permission denied', createdbresult)

        self.log.info('------------------------step6:验证不存在目录,若存在则删除--------------------')
        self.primary_root.sh('rm -rf ' + self.path)

        self.log.info('------------------------step7:主机使用新建不存在目录建立表空间 success-------------------------')
        createdb = f'''drop tablespace if exists dbsys026_tb3; create tablespace dbsys026_tb3 location '{self.path}';'''
        createdbresult = self.sh_primy.executDbSql(createdb)
        self.log.info(createdb)
        self.log.info(createdbresult)
        self.assertIn('CREATE TABLESPACE', createdbresult)

        self.log.info('------------------------step8:备机重建-----------------')
        rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG)
        self.log.info(rebuild_msg)
        self.assertTrue(rebuild_msg)

    def tearDown(self):
        self.log.info('----------------------this is tearDown----------------------------')
        self.log.info('chmod 700 ' + self.path)
        msg = self.primary_dbuser.sh('chmod 700 ' + self.path).result()
        self.log.info(msg)

        cmd = f'''drop tablespace if exists dbsys026_tbl;'''
        msg = self.sh_primy.executDbSql(cmd)
        self.log.info(msg)

        cmd = f'''drop tablespace if exists dbsys026_tb2;'''
        msg = self.sh_primy.executDbSql(cmd)
        self.log.info(msg)

        cmd = f'''drop tablespace if exists dbsys026_tb3;'''
        msg = self.sh_primy.executDbSql(cmd)
        self.log.info(msg)

        rm_cmd = f"rm -rf {self.path}; rm -rf {self.path1}"
        self.log.info(rm_cmd)
        msg = self.primary_root.sh(rm_cmd).result()
        self.log.info(msg)
        msg = self.standby1_root.sh(rm_cmd).result()
        self.log.info(msg)
        msg = self.standby2_root.sh(rm_cmd).result()
        self.log.info(msg)
